import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import { reactive, UnwrapNestedRefs } from "vue";

/**
 * 提示信息组件
 */
export default class TooltipComponent {
    private static instance: TooltipComponent = new TooltipComponent();
    /**
     * 提示信息组件
     */
    public static get Default(): TooltipComponent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);
    }

    private informations: UnwrapNestedRefs<Map<number, ClientMessage.TooltipInformation>> = reactive(new Map());

    /**
     * Count
     */
    public get Count(): number {
        return this.informations.size;
    }

    /**
     * Informations
     */
    public get Informations(): UnwrapNestedRefs<Map<number, ClientMessage.TooltipInformation>> {
        return this.informations;
    }


    public Add(information: ClientMessage.TooltipInformation): void {
        this.informations.set(information.InstanceId, information);
    }

    public AddRange(informations: Array<ClientMessage.TooltipInformation>): void {
        for (const information of informations) {
            this.informations.set(information.InstanceId, information);
        }
    }

    public Remove(instanceId: number): boolean {
        return this.informations.delete(instanceId);
    }

    public Update(input: ClientMessage.TooltipInformation): boolean {
        const information = this.informations.get(input.InstanceId);
        if (information == undefined) {
            this.informations.set(input.InstanceId, input);
            return false;
        }

        information.Text = input.Text;
        information.ViewportPointX = input.ViewportPointX;
        information.ViewportPointY = input.ViewportPointY;
        information.Version++;
        return true;
    }

    public Clear(): void {
        this.informations.clear();
    }
}